/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

/**
 *
 * @author mirko
 */


 import RPS.EquivalenceMapping;
import RPS.MappingAssertion;
import RPS.MappingVariableRenamer;
import RPS.PeerSchema;
import RPS.RPS;
import Rewriting.FederationManager;
import Rewriting.SPARQLEquivalenceChecker;
import SPARQL.Utilities.SPARQLVariableRenamer;
import Rewriting.SPARQLRewriter;
import Rewriting.Source;
import Rewriting.TripleUnifier;
import SPARQL.SPARQL;
import SPARQL.Service;
import SPARQL.SimpleURI;
import SPARQL.TriplePattern;
import SPARQL.Utilities.QueryRenamer;
import SPARQL.Utilities.SPARQLqueryTranslator;
import SPARQL.Variable;
import static Test.TestQuery.serviceEndpoint;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import java.util.HashSet;
import parser.ParseException;
import parser.SparqlParser;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.ResultSetRewindable;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author riccardo
 */
public class TestQuery1 {
    
    final static String serviceEndpoint = "http://sparql.org/sparql";
//final static String serviceEndpoint = "http://data.linkedmdb.org/sparql";
    final static String prefixes = "PREFIX lmDb: <http://data.linkedmdb.org/resource/> " + 
        "PREFIX lmDb.film: <http://data.linkedmdb.org/resource/film/> " +    
        "PREFIX dbpediaUk: <http://uk.dbpedia.org/resource/> " + 
        "PREFIX dbpedia: <http://dbpedia.org/resource/> "+
        "PREFIX dcterms: <http://purl.org/dc/terms/> " + 
        "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + 
        "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
        "PREFIX owl: <http://www.w3.org/2002/07/owl#> \n";
    
    final static String query1="SELECT ?x ?y WHERE { <http://data.linkedmdb.org/resource/film/21> ?x ?y } ";
    final static String query2="SELECT  ?y WHERE { <http://dbpedia.org/resource/Mulholland_Drive_(film)> <http://dbpedia.org/ontology/starring> ?y } ";
    //questa query come sopra ma qui si usano due vocabolari diversi, quindi nel caso normale la query risulterebbe nulla, ma nel peer sytsem abbiamo la risposta 
    //perche' vengono considerati tutti i nomi 
    final static String query3="SELECT  ?y WHERE { <http://data.linkedmdb.org/resource/film/21> <http://dbpedia.org/ontology/starring> ?y } ";
    //questa query come sopra query 2..solo cambia la direzione dei mappings 
    final static String query4="SELECT  ?y WHERE { <http://data.linkedmdb.org/resource/film/21> <http://data.linkedmdb.org/resource/movie/actor> ?y } ";
    //qui abbiamo l'inferenza di classe
    final static String query5="SELECT ?x  WHERE { ?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film> } ";
    //come sopra ma con tre mappings 
    final static String query6="SELECT ?x  WHERE { ?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://rdf.freebase.com/ns/film.film> } ";
    final static String query7="SELECT ?y  WHERE { <http://data.linkedmdb.org/resource/film/21> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?y } ";
    final static String query8="SELECT ?y ?z WHERE { <http://data.linkedmdb.org/resource/film/21> <http://data.linkedmdb.org/resource/movie/actor> ?y . ?y <http://dbpedia.org/ontology/birthDate> ?z} ";
    //stessa di sopra ma con 3 vocabolari nella query 
    final static String query9="SELECT ?y ?z WHERE { <http://data.linkedmdb.org/resource/film/21> <http://rdf.freebase.com/ns/film.film.starring> ?y . ?y <http://dbpedia.org/ontology/birthDate> ?z } ";
    
    
    public static void main(String[] args) throws ParseException, FileNotFoundException, UnsupportedEncodingException {
        
        
        ExecutionQuery exeq = new ExecutionQuery();
        System.out.println("Query 9");
        SPARQL parse = SparqlParser.parse(query9);
        TestFactory testFactory = new TestFactory();
        RPS peerSystem = testFactory.createRPS();
        FederationManager fedmanager = testFactory.createFederationManager();
       
        SPARQL queryTest = parse;
        SPARQL originalQuery = (SPARQL) queryTest.clone();
        
        
        
        System.out.println(peerSystem);
        Query originalQueryExe = QueryFactory.create(originalQuery.toString());
        System.out.println("Original query: " + QueryFactory.create(originalQuery.toString()));
        
        exeq.executeQueryOverOriginalSources(QueryFactory.create(originalQuery.toString()));
        
        
        SPARQLRewriter rewriter = new SPARQLRewriter(peerSystem);
        Set<SPARQL> setOfQueries = rewriter.rewrite(queryTest);
        
        
        
        
        SPARQLqueryTranslator translator = new SPARQLqueryTranslator();
        Set<SPARQL> setOfQueriesFed = fedmanager.rewriteMultiple(setOfQueries); //qui canbiare metodo in "rewrite" normale, se ci sono problemi con rewriteMultiple
        String finalqueryBody = translator.unionSetSparqlQueryBody(setOfQueriesFed);
        System.out.println("Set of query size: "+ setOfQueriesFed.size());
        
        
        String queryString = 
        prefixes +
        "SELECT  * " +  
      "   from <http://www.w3.org/2000/01/rdf-schema#> " +
        "   WHERE { "
            + finalqueryBody
  
            
            + " } ";
        /*
        String queryStringAsk = prefixes +
        "ASK " +  
      "   from <http://www.w3.org/2000/01/rdf-schema#> " +
        "    { "
            + finalqueryBody
  
            
            + " } ";
                */
    
    
    Query query = QueryFactory.create(queryString); 
    System.out.println(query);
    PrintWriter writer = new PrintWriter("queryfuffa.txt", "UTF-8");
    writer.println(query);
    writer.close();
// exception happens here
        
        exeq.executeQueryTest(query);
    

    
        
    System.out.println("\nall done.");
}

    
 
}
    
    


